package com.yy.jhl.ui.manager.activity;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.yy.common.base.BaseActivity;
import com.yy.common.commonutils.SPUtils;
import com.yy.common.commonutils.StringUtil;
import com.yy.common.commonwidget.OnNoDoubleClickListener;
import com.yy.jhl.R;
import com.yy.jhl.app.AppConstant;
import com.yy.jhl.bean.BatchInfo;
import com.yy.jhl.bean.ProductInfo;
import com.yy.jhl.ui.manager.adapter.BatchAdapter;
import com.yy.jhl.ui.manager.contract.BatchManagerContract;
import com.yy.jhl.ui.manager.presenter.BatchManagerPresenter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.yy.jhl.ui.main.activity.TakePhotoUploadActivity.UPLOAD_IMG;

/**
 * Created by libingn on 2017/12/7.
 */

public class BatchManagerActivity extends BaseActivity<BatchManagerPresenter> implements BatchManagerContract.View, AdapterView.OnItemClickListener, OnRefreshListener2 {

    TextView orderNameText;
    TextView orderCodeText;
    @BindView(R.id.batch_refresh_list_view)
    PullToRefreshListView batchRefreshListView;
    ListView batchListView;
    BatchAdapter batchAdapter;
    String orderId = "";
    String title = "";
    String orderSn = "";
    @BindView(R.id.ensure_btn)
    Button ensureBtn;
    @BindView(R.id.create_text)
    TextView createText;
    @BindView(R.id.no_data_parent)
    LinearLayout noDataParent;
    private int currentPage = 1;
    private int currentLoginType = 1;

    public static void startAction(Context mContext, String orderId, String title, String orderSn) {
        Intent intent = new Intent(mContext, BatchManagerActivity.class);
        intent.putExtra("orderId", orderId);
        intent.putExtra("title", title);
        intent.putExtra("orderSn", orderSn);
        mContext.startActivity(intent);
    }

    private void addHeadView() {
        View headerView = LayoutInflater.from(mContext).inflate(R.layout.header_act_batch_manager, null);
        orderNameText = (TextView) headerView.findViewById(R.id.order_name_text);
        orderCodeText = (TextView) headerView.findViewById(R.id.order_code_text);
        batchListView.addHeaderView(headerView);
    }

    @Override
    public int getLayoutId() {
        return R.layout.act_batch_manager;
    }

    @Override
    public void initView() {
        currentLoginType = SPUtils.getSharedIntData(mContext, AppConstant.SP_KEY_LOGIN_TYPE);
        orderId = getIntent().getStringExtra("orderId");
        title = getIntent().getStringExtra("title");
        orderSn = getIntent().getStringExtra("orderSn");
        batchRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
        batchListView = batchRefreshListView.getRefreshableView();
        batchAdapter = new BatchAdapter(mContext);
        batchListView.setAdapter(batchAdapter);
        batchRefreshListView.setOnRefreshListener(this);
        batchRefreshListView.setOnItemClickListener(this);
        addHeadView();
        showTitle("物流监管批次", new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                finish();
            }
        });

        orderNameText.setText(title);
        orderCodeText.setText(orderSn);
        mPresenter.getList(orderId, currentPage, true);
        if (currentLoginType == 1) {
            ensureBtn.setVisibility(View.VISIBLE);
            createText.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        batchAdapter.clearData();
        currentPage = 1;
        mPresenter.getList(orderId, currentPage, false);
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        currentPage += 1;
        mPresenter.getList(orderId, currentPage, false);

    }

    @Override
    public void getListSuccess(List<BatchInfo> orderInfos) {
        batchRefreshListView.onRefreshComplete();
        if (currentPage == 1) {
            batchAdapter.clearData();
            if (orderInfos == null ||orderInfos.size() == 0){
                noDataParent.setVisibility(View.VISIBLE);
                batchRefreshListView.setVisibility(View.GONE);
            }else {
                noDataParent.setVisibility(View.GONE);
                batchRefreshListView.setVisibility(View.VISIBLE);
            }
        }
        batchAdapter.addData(orderInfos);

    }

    @Override
    public void getListFail() {
        batchRefreshListView.onRefreshComplete();
    }
    @OnClick({R.id.create_text, R.id.ensure_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.create_text:
                //出库准备
                OutboundSubmitActivity.startAction(mContext, orderId,100);
                break;
            case R.id.ensure_btn:
                //出库准备
                OutboundSubmitActivity.startAction(mContext, orderId,100);
                break;
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 100:
                    boolean isSubmitData = data.getBooleanExtra("isSubmitData",false);
                    if (isSubmitData){
                        batchAdapter.clearData();
                        currentPage = 1;
                        mPresenter.getList(orderId, currentPage, false);
                    }
                    break;
            }
        }
    }
}
